<?php
/* 
 * 分类目录 操作类
 * 程序编写：pennyowen/pepsi.zh
 * 联系邮箱：penyowen#gmail.com/pepsi.zh#gmail.com
 * 修改时间：2008-11-1
 */
if (!defined('IN_SITE')){ die('Hacking attempt!'); }
interface CatalogDbInter{
    ####--------------------分类目录管理------------------------------####
    #### 注意 ： 大类改小类的适合一定要小心，很可能会出错 #####
    ### 功能：提交一个分类类型记录  参数：$post 提交的内容 #####
    function submitTypeRecord($post);
    ### 功能：删除分类类型记录  参数：$id 分类类型id #####
    function deleteTypeRecord($id);
    ### 功能：得到分类类别列表  参数：$type 分类类别的类型 0首页推荐类型 1为分类类型 #####
    function getTypeList($type=0);
    ### 功能：通过一个记录id获得一条分类类型记录  参数：$id 记录id #####
    function getOneType($id);
}
class catalog implements CatalogDbInter
{
	public $error = array();
	public $action = '提交';

    ####--------------------分类目录管理------------------------------####
    function submitTypeRecord($post){
    	global $dbconn,$dbtable;
    	$post = acrossTrim($post);
    	$flag = true;
    	if (strlen($post[Categoryname]) < 1){
    		$flag = false;
			$this->error[e_Categoryname] = '分类类别名称不能为空';
    	}
    	if ($flag){
    		if ($post[Catalogid]){
    			$this->action = '修改';
    			$query = "select Catalogid from ".$dbtable->cata." where Categoryname='".$post[Categoryname]."' and Catalogid!=".$post[CAid];
    			if ($dbconn->Getonerecord($query)){
	    			$flag = false;
					$this->error[e_Categoryname] = '分类类别名称已经存在了，请换一个';
    			}else {
    				///////为#remarks 001做准备
    				$query = "select CparentId from ".$dbtable->cata." where Catalogid=".$post[Catalogid]." limit 1";
    				$CparentId = $dbconn->Getonefield($query);
    				$sunType = $this->getTypeList($post[Catalogid],0);
    				////////如果是父类是自己本身就注销父类值
					if ($post[CparentId] == $post[Catalogid]){
    					unset($post[CparentId]);
    				}
					if ($dbconn->Query_update($post,$dbtable->cata,array('Catalogid'=>$post['Catalogid']))){
						///#remarks 001///大类改小类，并且还是在同一类别所属下面的的时候。要做下面的处理
    				 	if (array_key_exists($post[CparentId],$sunType)){
    				 		$query = "update ".$dbtable->cata." set CparentId=$CparentId where CparentId=".$post[Catalogid];
    						$dbconn->Query($query);
    				 	}
    				 	return true;
    				 }else {
    				 	return false;
    				 }
    			}
    		}else {
    			$this->action = '添加';
    			$query = "select Catalogid from ".$dbtable->cata." where Categoryname='".$post[Categoryname]."'";
    			if ($dbconn->Getonerecord($query)){
	    			$flag = false;
					$this->error[e_Categoryname] = '分类类别名称已经存在了，请换一个';
    			}else {
    				return $dbconn->Query_adate($post,$dbtable->cata);
    			}
    		}
    	}
    	return false;
    }
    function deleteTypeRecord($id){
    	global $dbconn,$dbtable;
    	if (empty($id)) { return false ;}
    	else { $id = intval($id); }
    	$query = "select CparentId from ".$dbtable->cata." where Catalogid=$id";
    	$CparentId = $dbconn->Getonefield($query);
    	$query = "update ".$dbtable->cata." set CparentId=$CparentId where CparentId=$id";
    	$dbconn->Query($query);
    	return deleteRecord($dbtable->cata,"Catalogid=$id");
    }
    function getTypeList($CparentId=0,$returnType=1,$num=0)
	{
    	global $dbconn,$dbtable;
    	$array = array();
    	$query = "select Catalogid,Categoryname from ".$dbtable->cata." where CparentId='$CparentId' order by Corder asc, Catalogid asc";
    	$nowQueryid = $dbconn->Query($query);
    	if ($returnType){
	    	for ($num_i=0;$num_i<$num;$num_i++){
	    		$cataname .= '&nbsp;&nbsp;';
	    	}
	    	if ($cataname)
	    	$cataname .= '|--';
    	}
    	++$num;
    	while (($row = $dbconn->next_record($nowQueryid))){
    		$array[$row[Catalogid]] = $cataname.$row[Categoryname];
    		if (($min_array = $this->getTypeList($row[Catalogid],$returnType,$num))){
    			$array = $array+$min_array;
    		}
    	}
    	return $array;
    }
    function getOneType($id){
    	global $dbconn,$dbtable;
    	if (empty($id)){ return false ;}
    	$query = "select * from ".$dbtable->cata." where Catalogid=$id limit 1";
    	return $dbconn->Getonerecord($query);
    }
}
?>